package demo.activity;

import java.util.ArrayList;

import util.MLog;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import demo.db.bean.City;
import demo.db.bean.Country;
import demo.db.bean.Province;
import demo.db.util.AssetsDatabaseManager;
import demo.zandroid.R;

public class DataSelectActivity extends Activity {

	private AssetsDatabaseManager adm;
	private String databaseName = "dbs/diqu.db";
	private SQLiteDatabase db;

	private ArrayList<Province> provinces = new ArrayList<Province>();
	private ArrayList<City> cities = new ArrayList<City>();
	private ArrayList<Country> countries = new ArrayList<Country>();
	
	private Spinner spinner1,spinner2,spinner3;
	private TextView textView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_data);
		
		spinner1 = (Spinner) findViewById(R.id.spinner1);
		spinner2 = (Spinner) findViewById(R.id.spinner2);
		spinner3 = (Spinner) findViewById(R.id.spinner3);
		textView = (TextView) findViewById(R.id.textView1);
		
		adm = AssetsDatabaseManager.getManager();
		db = adm.getDatabase(databaseName);
		
		getProvince();
		
		MLog.i(getClass(), ""+provinces);
	}

	private void getProvince() {
		Province province;
		if (db != null) {
			provinces.clear();
			Cursor cursor = db.query("province",
					new String[] { "id", "name" },
					null, null, null, null, null, null);
			if(cursor != null && cursor.getCount()>0){
				while(cursor.moveToNext()){
					province = new Province();
					province.id = cursor.getInt(cursor.getColumnIndex("id"));
					province.name = cursor.getString(cursor.getColumnIndex("name"));
					provinces.add(province);
				}
			}
			cursor.close();
		}
		spinner1.setAdapter(new ArrayAdapter<Province>(this, 
				android.R.layout.simple_list_item_1, 
				provinces));
		spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				Province province = (Province) parent.getItemAtPosition(position);
				getCities(province.id+"");
				
				textView.setText(province.name);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
		
	}

	private void getCities(String pid) {
		City city;
		if (db != null) {
			cities.clear();
			Cursor cursor = db.query("city",
					new String[] { "id", "name", "pid" }, 
					"pid=?", new String[] { pid }, 
					null, null, null, null);
			if(cursor != null && cursor.getCount()>0){
				while(cursor.moveToNext()){
					city = new City();
					city.id = cursor.getInt(cursor.getColumnIndex("id"));
					city.name = cursor.getString(cursor.getColumnIndex("name"));
					city.pid = cursor.getInt(cursor.getColumnIndex("pid"));
					cities.add(city);
				}
			}
			cursor.close();
			if(cities.size() == 0){
				countries.clear();
				spinner3.setAdapter(new ArrayAdapter<Country>(this, 
				android.R.layout.simple_list_item_1, 
				countries));
			}
		}
		spinner2.setAdapter(new ArrayAdapter<City>(this, 
				android.R.layout.simple_list_item_1, 
				cities));
		spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				City city = (City) parent.getItemAtPosition(position);
				getCountries(city.id+"");
				String text = spinner1.getSelectedItem()+","+city.name;
				textView.setText(text);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
	}

	private void getCountries(String cid) {
		Country country;
		if (db != null) {
			countries.clear();
			Cursor cursor = db.query("country",
					new String[] { "id", "name", "cid" },
					"cid=?", new String[] { cid },
					null, null, null, null);
			if(cursor != null && cursor.getCount()>0){
				while(cursor.moveToNext()){
					country = new Country();
					country.id = cursor.getInt(cursor.getColumnIndex("id"));
					country.name = cursor.getString(cursor.getColumnIndex("name"));
					country.cid = cursor.getInt(cursor.getColumnIndex("cid"));
					countries.add(country);
				}
			}
			cursor.close();
		}
		spinner3.setAdapter(new ArrayAdapter<Country>(this, 
				android.R.layout.simple_list_item_1, 
				countries));
		spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				Country country = (Country) parent.getItemAtPosition(position);
				String text = spinner1.getSelectedItem()+","
						+spinner2.getSelectedItem()+","+country.name;
				textView.setText(text);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(adm != null){			
			adm.closeDatabase(databaseName);
		}
	}
}
